package com.xmb.wechat.view.alipay;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.blankj.utilcode.util.ToastUtils;
import com.nil.sdk.utils.DateUtils;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.application.WechatStartApplication;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.AliPayBillBean;
import com.xmb.wechat.bean.BankCardBean;
import com.xmb.wechat.bean.WechatContactBean;
import com.xmb.wechat.common.Constant;
import com.xmb.wechat.definterface.CommonIAndSListener;
import com.xmb.wechat.dialog.CommonListDialog;
import com.xmb.wechat.helper.DataHelper;
import com.xmb.wechat.util.PicChooseUtils;
import com.xmb.wechat.util.PicLoadUtils;
import com.xmb.wechat.view.wechat.money.WechatFundBankCardActivity;
import com.xmb.wechat.widget.InputDialog;
import com.xmb.wechat.widget.TitleLayout;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author lijiangop
 * @CreateTime 2020/5/25 16:10
 */
public class AliPayBillAddActivity extends BaseActivity {
    private static final int REQUEST_CODE_BANK_CARD = 002;
    //common
    @BindView(R2.id.title_layout)
    TitleLayout  mTitleLayout;
    //网购
    @BindView(R2.id.tv_wg_platform)
    TextView     mTvWgPlatform;
    @BindView(R2.id.ll_wg_platform)
    LinearLayout mLlWgPlatform;
    @BindView(R2.id.et_wg_name)
    EditText     mEtWgName;
    @BindView(R2.id.et_wg_money)
    EditText     mEtWgMoney;
    @BindView(R2.id.ll_type_wg)
    LinearLayout mLlTypeWg;
    //提现
    @BindView(R2.id.tv_tx_yhk)
    TextView     mTvTxYhk;
    @BindView(R2.id.ll_tx_yhk)
    LinearLayout mLlTxYhk;
    @BindView(R2.id.et_tx_money)
    EditText     mEtTxMoney;
    @BindView(R2.id.ll_type_tx)
    LinearLayout mLlTypeTx;
    //红包
    @BindView(R2.id.tv_hb_expend)
    TextView     mTvHbExpend;
    @BindView(R2.id.tv_hb_income)
    TextView     mTvHbIncome;
    @BindView(R2.id.et_hb_money)
    EditText     mEtHbMoney;
    @BindView(R2.id.ll_type_hb)
    LinearLayout mLlTypeHb;
    //转账
    @BindView(R2.id.tv_zz_object)
    TextView     mTvZzObject;
    @BindView(R2.id.iv_zz_object)
    ImageView    mIvZzObject;
    @BindView(R2.id.ll_zz_object)
    LinearLayout mLlZzObject;
    @BindView(R2.id.et_zz_real_name)
    EditText     mEtZzRealName;
    @BindView(R2.id.tv_zz_expend)
    TextView     mTvZzExpend;
    @BindView(R2.id.tv_zz_income)
    TextView     mTvZzIncome;
    @BindView(R2.id.et_zz_money)
    EditText     mEtZzMoney;
    @BindView(R2.id.ll_type_zz)
    LinearLayout mLlTypeZz;
    //充值
    @BindView(R2.id.et_cz_money)
    EditText     mEtCzMoney;
    @BindView(R2.id.ll_type_cz)
    LinearLayout mLlTypeCz;
    //花呗
    @BindView(R2.id.et_hbhk_title)
    EditText     mEtHbhkTitle;
    @BindView(R2.id.et_hbhk_money)
    EditText     mEtHbhbMoney;
    @BindView(R2.id.ll_type_hbhk)
    LinearLayout mLlTypeHbhk;
    //自定义
    @BindView(R2.id.iv_zdy_pic)
    ImageView    mIvZdyPic;
    @BindView(R2.id.et_zdy_title)
    EditText     mEtZdyTitle;
    @BindView(R2.id.et_zdy_money)
    EditText     mEtZdyMoney;
    @BindView(R2.id.ll_type_zdy)
    LinearLayout mLlTypeZdy;
    //common
    @BindView(R2.id.tv_bill_time)
    TextView     mTvBillTime;
    @BindView(R2.id.tv_bill_classify)
    TextView     mTvBillClassify;
    @BindView(R2.id.sw_is_show_trade_state)
    Switch       mSwIsShowTradeState;
    @BindView(R2.id.tv_bill_state_type)
    TextView     mTvBillStateType;
    @BindView(R2.id.ll_bill_state_type)
    LinearLayout mLlBillStateType;
    @BindView(R2.id.et_bill_state_value)
    EditText     mEtBillStateValue;
    @BindView(R2.id.ll_bill_state_value)
    LinearLayout mLlBillStateValue;

    private int              mType;
    private AliPayBillBean   mBillBean;
    private CommonListDialog mDialog;

    public AliPayBillAddActivity() {
        super(R.layout.activity_alipay_bill_add);
    }

    public static void start(Activity activity, int type, int requestCode) {
        Intent intent = new Intent(activity, AliPayBillAddActivity.class);
        intent.putExtra("type", type);
        activity.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void initView() {
        mTitleLayout.setRightClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doConfirm();
            }
        });
        mSwIsShowTradeState.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mLlBillStateType.setVisibility(isChecked ? View.VISIBLE : View.GONE);
                mLlBillStateValue.setVisibility(isChecked ? View.VISIBLE : View.GONE);
                mBillBean.setShowTradeState(isChecked);
            }
        });
        mBillBean = new AliPayBillBean();
        mType = getIntent().getIntExtra("type", Constant.AliPayBillType.TYPE_ZDY);
        mBillBean.setType(mType);
        switch (mType) {
            case Constant.AliPayBillType.TYPE_ZDY:
                mLlTypeZdy.setVisibility(View.VISIBLE);
                mTitleLayout.setTitle("自定义");
                showTag("其他");
                break;
            case Constant.AliPayBillType.TYPE_WG:
                mLlTypeWg.setVisibility(View.VISIBLE);
                mTitleLayout.setTitle("网购");
                //平台
                mBillBean.setPlatformType("淘宝");
                //分类
                showTag("服饰美容");
                //图片
                mTvWgPlatform.setText("淘宝");
                setIcon(Constant.SourceType.AVATAR_SRC_APP_INNER_DRAWABLE, String.valueOf(R.drawable.alipay_bill_taobao));
                break;
            case Constant.AliPayBillType.TYPE_TX:
                mLlTypeTx.setVisibility(View.VISIBLE);
                mTitleLayout.setTitle("提现");
                //分类
                showTag("转账充值");
                break;
            case Constant.AliPayBillType.TYPE_HB:
                mLlTypeHb.setVisibility(View.VISIBLE);
                setHbState(true);
                mTitleLayout.setTitle("红包");
                //分类
                showTag("人情往来");
                //图片
                setIcon(Constant.SourceType.AVATAR_SRC_APP_INNER_DRAWABLE, String.valueOf(R.drawable.alipay_bill_hongbao));
                break;
            case Constant.AliPayBillType.TYPE_ZZ:
                mLlTypeZz.setVisibility(View.VISIBLE);
                mTitleLayout.setTitle("转账");
                //分类
                showTag("转账充值");
                break;
            case Constant.AliPayBillType.TYPE_SJCZ:
                mLlTypeCz.setVisibility(View.VISIBLE);
                mTitleLayout.setTitle("手机充值");
                //分类
                showTag("通讯物流");
                //图片
                setIcon(Constant.SourceType.AVATAR_SRC_APP_INNER_DRAWABLE, String.valueOf(R.drawable.alipay_bill_shouji));
                break;
            case Constant.AliPayBillType.TYPE_HBHK:
                mLlTypeHbhk.setVisibility(View.VISIBLE);
                mTitleLayout.setTitle("花呗还款");
                //default title
                mEtHbhkTitle.setText("主动还款-花呗" + new SimpleDateFormat("yyyy年MM月").format(new Date()) + "账单");
                //分类
                showTag("信用借还");
                //图片
                setIcon(Constant.SourceType.AVATAR_SRC_APP_INNER_DRAWABLE, String.valueOf(R.drawable.alipay_bill_huabei));
                break;
        }
        setIsExpend(true);
        showTime(new Date());
    }

    @OnClick({R2.id.ll_wg_platform, R2.id.ll_tx_yhk, R2.id.ll_zz_object, R2.id.tv_zz_expend, R2.id.tv_zz_income, R2.id.tv_hb_expend,
            R2.id.tv_hb_income, R2.id.ll_bill_time, R2.id.ll_bill_classify, R2.id.ll_bill_state_type, R2.id.ll_zdy_pic, R2.id.btn_preview})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.ll_wg_platform) {
            showListDialog(DataHelper.getWgPLatformData(), new CommonIAndSListener() {
                @Override
                public void onChoose(int i, String s) {
                    if (TextUtils.isEmpty(s))
                        return;
                    mTvWgPlatform.setText(s);
                    mBillBean.setPlatformType(s);
                    setIcon(Constant.SourceType.AVATAR_SRC_APP_INNER_DRAWABLE, TextUtils.equals(s, "淘宝") ? String.valueOf(R.drawable.alipay_bill_taobao) : String.valueOf(R.drawable.alipay_bill_tianmao));
                }
            });
        } else if (id == R.id.ll_tx_yhk) {
            WechatFundBankCardActivity.startforResult(this, REQUEST_CODE_BANK_CARD);
        } else if (id == R.id.ll_zz_object) {
            showRoleChooseSender(this, false, false, null);
        } else if (id == R.id.tv_zz_expend) {
            setIsExpend(true);
        } else if (id == R.id.tv_zz_income) {
            setIsExpend(false);
        } else if (id == R.id.tv_hb_expend) {
            setHbState(true);
        } else if (id == R.id.tv_hb_income) {
            setHbState(false);
        } else if (id == R.id.ll_bill_time) {
            showTimeChoose(new OnTimeSelectListener() {
                @Override
                public void onTimeSelect(Date date, View v) {
                    showTime(date);
                }
            });
        } else if (id == R.id.ll_bill_classify) {
            InputDialog.showInputDialog(this, "账单分类", "其他", EditorInfo.TYPE_CLASS_TEXT, new InputDialog.OnInputDialogListener() {
                @Override
                public void onCancelClicked() {
                }

                @Override
                public void onOKClicked(Dialog dialog, String inputText) {
                    dialog.dismiss();
                    showTag(inputText);
                }
            });
        } else if (id == R.id.ll_bill_state_type) {
            showListDialog(DataHelper.getBillStateType(), new CommonIAndSListener() {
                @Override
                public void onChoose(int i, String s) {
                    if (TextUtils.isEmpty(s))
                        return;
                    mTvBillStateType.setText(s);
                    mBillBean.setTradeState(i);
                    mBillBean.setTradeStateValue(s);
                }
            });
        } else if (id == R.id.ll_zdy_pic) {
            PicChooseUtils.chooseOnePicWithCut(this, 1, 1, Pic -> {
                PicLoadUtils.loadSdcardPic(Pic[0], mIvZdyPic);
                setIcon(Constant.SourceType.AVATAR_SRC_SDCARD, Pic[0]);
            });
        } else if (id == R.id.btn_preview) {
            doConfirm();
        }
    }

    //--------------------------------------------------------------------arg set start-----------------------------------------------------------
    private void setIcon(int sourceType, String source) {
        mBillBean.setIconSourceType(sourceType);
        mBillBean.setIconUrl(source);
    }

    private void showTag(String tag) {
        mTvBillClassify.setText(tag);
        mBillBean.setBillClassify(tag);
    }

    private void showTime(Date date) {
        mTvBillTime.setText(DateUtils.getStringByFormat(date, "yyyy-MM-dd HH:mm:ss"));
        mBillBean.setTradeTime(date.getTime());
    }

    private void setHbState(boolean isExpend) {
        mTvHbExpend.setSelected(isExpend);
        mTvHbIncome.setSelected(!isExpend);
        mBillBean.setExpend(isExpend);
    }

    private void setIsExpend(boolean isExpend) {
        mTvZzExpend.setSelected(isExpend);
        mTvZzIncome.setSelected(!isExpend);
        mBillBean.setExpend(isExpend);
    }
    //--------------------------------------------------------------------arg set end-----------------------------------------------------------

    /**
     * 选择时间
     *
     * @param listener
     */
    private void showTimeChoose(OnTimeSelectListener listener) {
        @SuppressLint("SimpleDateFormat")
        TimePickerView pvTime = new TimePickerBuilder(this, listener).setType(new boolean[]{true, true, true, true, true, true})// 默认全部显示
                .setCancelText("取消")//取消按钮文字
                .setSubmitText("确定")//确认按钮文字
                .setTitleSize(20)//标题文字大小
                .setTitleText("时间")//标题文字
                .setOutSideCancelable(true)//点击屏幕，点在控件外部范围时，是否取消显示
                .isCyclic(true)//是否循环滚动
                .setSubmitColor(getResources().getColor(R.color.wechat_tab_bg_green))//确定按钮文字颜色
                .setCancelColor(getResources().getColor(R.color.wechat_tab_bg_green))//取消按钮文字颜色
                .setLabel("年", "月", "日", "时", "分", "秒")//默认设置为年月日时分秒
                .isCenterLabel(true) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .build();
        pvTime.show();
    }

    /**
     * 点击确定
     */
    private void doConfirm() {
        AliPayBillBean aliPayBillBean = checkValueAvaliable();
        if (aliPayBillBean == null)
            return;
        Intent intent = new Intent();
        setResult(RESULT_OK, intent);
        WechatStartApplication.getBoxStore().boxFor(AliPayBillBean.class).put(aliPayBillBean);
        this.finish();
    }

    /**
     * 检查参数是否满足
     *
     * @return
     */
    private AliPayBillBean checkValueAvaliable() {
        String money = "";
        String title = "";
        switch (mType) {
            case Constant.AliPayBillType.TYPE_ZDY:
                if (TextUtils.isEmpty(mBillBean.getIconUrl())) {
                    ToastUtils.showShort("请选择交易类型图片");
                    return null;
                }
                title = mEtZdyTitle.getText().toString().trim();
                if (TextUtils.isEmpty(title)) {
                    ToastUtils.showShort("请填写交易标题");
                    return null;
                }
                money = mEtZdyMoney.getText().toString().trim();
                if (TextUtils.isEmpty(money)) {
                    ToastUtils.showShort("请填写交易金额");
                    return null;
                }
                break;
            case Constant.AliPayBillType.TYPE_WG:
                if (TextUtils.isEmpty(mBillBean.getPlatformType())) {
                    ToastUtils.showShort("请选择平台类型");
                    return null;
                }
                title = mEtWgName.getText().toString().trim();
                if (TextUtils.isEmpty(title)) {
                    ToastUtils.showShort("请填写商品名称");
                    return null;
                }
                money = mEtWgMoney.getText().toString().trim();
                if (TextUtils.isEmpty(money)) {
                    ToastUtils.showShort("请填写商品金额");
                    return null;
                }
                break;
            case Constant.AliPayBillType.TYPE_TX:
                String yhk = mTvTxYhk.getText().toString().trim();
                if (TextUtils.isEmpty(yhk) || TextUtils.equals(yhk, "请选择")) {
                    ToastUtils.showShort("请选择银行卡");
                    return null;
                }
                title = "余额提现";
                money = mEtTxMoney.getText().toString().trim();
                if (TextUtils.isEmpty(money)) {
                    ToastUtils.showShort("请填写提现金额");
                    return null;
                }
                break;
            case Constant.AliPayBillType.TYPE_HB:
                title = "发送普通红包";
                money = mEtHbMoney.getText().toString().trim();
                if (TextUtils.isEmpty(money)) {
                    ToastUtils.showShort("请填写红包金额");
                    return null;
                }
                break;
            case Constant.AliPayBillType.TYPE_ZZ:
                String objectName = mTvZzObject.getText().toString().trim();
                if (TextUtils.isEmpty(objectName) || TextUtils.equals("请选择", objectName)) {
                    ToastUtils.showShort("请选择转账对象");
                    return null;
                }
                String realName = mEtZzRealName.getText().toString().trim();
                title = TextUtils.isEmpty(realName) ? "转账-" + objectName : "转账-" + objectName + "(" + realName + ")";
                money = mEtZzMoney.getText().toString().trim();
                if (TextUtils.isEmpty(money)) {
                    ToastUtils.showShort("请填写转账金额");
                    return null;
                }
                break;
            case Constant.AliPayBillType.TYPE_SJCZ:
                title = "手机充值";
                money = mEtCzMoney.getText().toString().trim();
                if (TextUtils.isEmpty(money)) {
                    ToastUtils.showShort("请填写充值金额");
                    return null;
                }
                break;
            case Constant.AliPayBillType.TYPE_HBHK:
                title = mEtHbhkTitle.getText().toString().trim();
                if (TextUtils.isEmpty(title)) {
                    ToastUtils.showShort("请填写花呗标题");
                    return null;
                }
                money = mEtHbhbMoney.getText().toString().trim();
                if (TextUtils.isEmpty(money)) {
                    ToastUtils.showShort("请填写还款金额");
                    return null;
                }
                break;
        }
        if (mBillBean.isShowTradeState()) {
            if (TextUtils.isEmpty(mTvBillStateType.getText().toString().trim())) {
                ToastUtils.showShort("请选择交易状态类型");
                return null;
            }
            String trim = mEtBillStateValue.getText().toString().trim();
            if (TextUtils.isEmpty(trim)) {
                ToastUtils.showShort("请填写交易状态内容");
                return null;
            }
            mBillBean.setTradeStateValue(trim);
        }
        mBillBean.setTitle(title);
        mBillBean.setMoney(money);
        return mBillBean;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null)
            return;
        if (requestCode == REQUEST_CODE_BANK_CARD) {//银行卡
            BankCardBean bankCardBean = (BankCardBean) data.getSerializableExtra("result");
            if (bankCardBean == null)
                return;
            String bankcard = bankCardBean.getBankName() + "(" + bankCardBean.getBankNumber() + ")";
            mTvTxYhk.setText(bankcard);
            setIcon(Constant.SourceType.AVATAR_SRC_DEFAULT, bankCardBean.getAvatar());

        } else if (requestCode == REQUEST_CODE_CHOOSE_OTHER_SEND || requestCode == REQUEST_CODE_CHOOSE_NEW_ADD_SEND) {//转账对象
            WechatContactBean result = (WechatContactBean) data.getSerializableExtra("result");
            if (result == null)
                return;
            mTvZzObject.setText(result.getName());
            PicLoadUtils.setupRoundPicIntoImageView(result.getAvatarSrc(), result.getAvatar(), mIvZzObject);
            setIcon(result.getAvatarSrc(), result.getAvatar());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
    }
}
